package com.xoa.util;


import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;


public class Patch2 {
    static int fileCount = 0;//记录总共拉了多少文件
    static int directoryCount = 0;//记录总共涉及了多少文件夹

    /**
     * ftl,js,controller到dao层,po,bo都要复制出来
     * 根据destPath,configFileName,srcPath三个地方设置,将config.txt中的文件按路径复制到对应的文件夹下.
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        String destPath = "E:\\servers\\a-ctoa";//黏贴文件的目标路径,,需要修改
        String configFileName = "config.txt";//配置文件名,用于存放需要复制的文件名和文件夹名,需要修改
        String srcPath = new String("E:\\a-qxcl\\xtdoa\\xoa\\out");//原文件夹目录,conch的上一级目录,需要修改

        srcPath = srcPath.replace("\\", "\\\\");
        final int srcFolderPrefixLength = srcPath.length();//每一行绝对路径要删除的前缀的字符数量

        File destFolder = new File(destPath);//目标文件夹
        File[] files = destFolder.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                //删除目标路径中的文件夹
                FileUtils.forceDelete(file);
            }
        }

        String configFilePath = destPath + "\\" + configFileName;//配置文件绝对路径
        Path path = Paths.get(configFilePath);//需要复制的文件配置
        List<String> lines = Files.readAllLines(path);

        //复制文件到目标路径
        for (String line : lines) {
            if (line.length() == 0) {
                continue;
            }

            File file = new File(line);
            line = line.replace("\\", "\\\\");
            if (file.isDirectory()) {
                copyFile(file, destPath, srcFolderPrefixLength);
            } else {
                FileUtils.copyFile(file, new File(destPath + line.substring(srcFolderPrefixLength)));
            }
        }

        //待计数的文件夹路径
        String countPath = destPath + "\\production";
        File countDirectory = new File(countPath);
        countFile(countDirectory);
        System.out.println("该补丁文件共" + fileCount + "个");
        System.out.println("该补丁文件夹共" + directoryCount + "个");
        System.out.println("po,bo是否复制?");
        System.out.println("字段是否加入?");
        System.out.println("js中的debugger是否均移除?");
        System.out.println("菜单的权限都加入没?");
        System.out.println("角色的权限都配置没?");
    }

    private static void countFile(File directory) {
        File[] fileList = directory.listFiles();
        for (File file : fileList) {
            if (file.isDirectory()) {
                directoryCount += 1;
                countFile(file);
            } else {
                fileCount += 1;
            }
        }
    }

    private static void copyFile(File directory, String destPath, int srcFolderPrefixLength) throws IOException {
        File[] fileList = directory.listFiles();
        for (File file : fileList) {
            if (file.isDirectory()) {
                copyFile(file, destPath, srcFolderPrefixLength);
            } else {
                String srcFilePath = file.getAbsolutePath().replace("\\", "\\\\");
                FileUtils.copyFile(file, new File(destPath + srcFilePath.substring(srcFolderPrefixLength)));
            }
        }
    }

}